<?php
namespace Common\Model;

use Think\Model;

class LogLoginModel extends Model
{
    protected $_validate = array(//array('title','require','问题标题必须填写！',1),
    );

    protected $_auto = array(//array('issolve','0'),
    );

    /**
     * 获取列表
     *
     * @param string $uid
     * @param bool $page
     * @param int $limit
     * @return mixed
     */
    public function log_list($uid = '', $page = true, $limit = 10)
    {
        $uid = intval($uid);
        if ($uid > 0) {
            $map['uid'] = $uid;
        }
        if ($page) {
            $count = $this->where($map)->count();
            $Page = pager($count, $limit);
            $list = $this->where($map)->order('id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        } else {
            $list = $this->where($map)->order('id DESC')->limit($limit)->select();
        }
        $return['list'] = $list;
        $return['count'] = $page ? $count : count($list);
        $return['page'] = $page ? $Page->fshow() : '';
        return $return;
    }

    /**
     * 记录用户登录日志
     *
     * @param string $uid
     * @return bool
     */
    public function add_login_log($uid = '')
    {
        $uid = intval($uid);
        if (empty($uid)) {
            return false;
        } else {
            $info = $this->where('uid=' . $uid)->order('id desc')->find();//查询最近登录数据
            if (!empty($info)) {
                D('User')->edit_login_lastlog($info);//最近的更新到会员最后登录
            }
            $data['uid'] = $uid;
            $data['login_time'] = time();
            $data['login_ip'] = get_client_ip();
            $data['login_address'] = get_client_ip_txt();
            $data['warning'] = 0;
            $data['entrance'] = C('YTATA_ENTRANCE');
            if ($data['login_ip'] <> $info['login_ip']) {
                $data['warning'] = 1;
            }
            $this->add($data);
            D('Task')->exe_first_login($uid);//执行每日第一次登录任务
            return true;
        }
    }

}